package b3.widget.guide
{
	import b3.net.*;
	import b3.util.*;
	import b3.widget.basic.*;
	
	import com.greensock.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.filters.*;
	import flash.text.*;
	
	public class TalkDialog extends BasicWidget
	{
		private var content:Array;
		private var contentIndex:int;
		private var contentText:TextField;
		private var showLength:int;
		private var buttonOK:BasicButton;
		
		public function TalkDialog(dialogBgrName:String, content:Array)
		{
			super();
			if (content == null || content.length == 0)
			{
				setContent([""]);
			}
			else
			{
				setContent(content);
			}
			
			//对话框背景
			var DialogBgr:Class = ResourceHelper.getInstance().getClassByName(dialogBgrName);
			addChild(new DialogBgr());
			//对话
			contentText = new TextField();
			contentText.selectable = false;
        	contentText.autoSize = TextFieldAutoSize.LEFT;
        	contentText.multiline = true;
        	var contentFormat:TextFormat = new TextFormat(BFont.fontName, 20, 0x00AAFF, true);
        	contentFormat.letterSpacing = 1;
        	contentFormat.leading = 10;
			contentText.defaultTextFormat = contentFormat;
//			contentText.filters = [new GlowFilter(0x00AAFF, 1, 1.3, 1.3, 100, BitmapFilterQuality.HIGH)];
			contentText.x = 40;
			contentText.y = 35;
			addChild(contentText);
			contentIndex = 0;

			var ButtonBgr:Class = ResourceHelper.getInstance().getClassByName("ButtonBgr0");
			buttonOK = new BasicButton(ButtonBgr, "确定");
			buttonOK.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			buttonOK.x = 160;
			buttonOK.y = 170;
			addChild(buttonOK);
			
			this.addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
			this.addEventListener(Event.REMOVED_FROM_STAGE, onRemovedFromStage);
		}
		
		public function addButton(button:BasicButton):void
		{
			button.addEventListener(MouseEvent.CLICK, onButtonOKClick);
			button.x = 100;
			button.y = 150;
			addChild(button);
			buttonOK.x = 220;
			buttonOK.y = 150;
		}
		
		private function onButtonOKClick(event:MouseEvent):void
		{
			if (contentIndex < content.length - 1)
			{
				contentIndex++;
				showLength = 0;
			}
			else
			{
				close();
			}	
			event.stopImmediatePropagation();
		}
		
		private function onAddedToStage(event:Event):void
		{
			addEventListener(Event.ENTER_FRAME, onEnterFrame, false, 0, true);
		}

		private function onRemovedFromStage(event:Event):void
		{
			removeEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
	
		private function onEnterFrame(event:Event):void
		{
			var contentStr:String = content[contentIndex];
			contentStr = contentStr.substr(0, showLength++);
			contentText.htmlText = contentStr;
		}
		
		private function setContent(content:Array):void
		{
			this.content = new Array();
			for (var i:int = 0; i < content.length; i++)
			{
				var contentStr:String = content[i];
				contentStr = "        " + contentStr.slice(0, 10) + "<br>" + contentStr.slice(10, 25)
					 + "<br>    " + contentStr.slice(25, 37);
				this.content.push(contentStr); 
			}
		}
		
	}
}